package com.xuyiwei.mayi.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by xuyiwei on 2019/4/13.
 */
public class BlockQueueRestrant {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue(3);
        Thread cook = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        queue.put("aaaa");
                        System.out.println("制作了一份食物，还剩"+queue.size()+"份食物");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        Thread eat = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        queue.take();
                        System.out.println("吃了一份食物，还剩" + queue.size() + "份食物");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        cook.start();
        eat.start();

    }
}
